package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiniuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * @author Dugen.Zhu
 * @date 2020-01-02 8:59
 */
public class ClearImgJob {

    @Autowired
    private JedisPool jedisPool;

    public void clearImg() {
        Jedis jedis = jedisPool.getResource();
        Set<String> set = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        if (set != null) {
            for (String filName : set) {
                //从七牛云删除这个filName
                QiniuUtils.deleteFileFromQiniu(filName);
                //删除集合中的这个文件名
                jedis.srem(RedisConstant.SETMEAL_PIC_RESOURCES, filName);
                System.out.println("清理" + filName + "垃圾图片成功");
            }
        }
    }
}
